package com.esd.device.utils;

public class NumberUtils {

    // 16 进制字符串转十进制
    public static int hexToInt(String str) {
        return Integer.parseInt(str, 16);
    }

    public static long hexToLong(String str) {
        return Long.parseLong(str, 16);
    }

    // 将16进制数解析为10进制，不足时前置位补0，主要用于时间的显示
    public static String hexToIntString(String str , int bits) {
        int decimal = Integer.parseInt(str, 16); // 将16进制字符串解析为10进制数
        StringBuilder result = new StringBuilder(decimal + "");
        if (result.length() < bits){
            int num = bits - result.length();
            for (int i = 0; i < num; i++) {
                result.insert(0, "0");
            }
        }else {
            return result.substring(result.length() - bits);
        }
        return result.toString();
    }

    public static String hexToLongString(String str , int bits) {
        long decimal = Long.parseLong(str, 16); // 将16进制字符串解析为10进制数
        StringBuilder result = new StringBuilder(decimal + "");
        if (result.length() < bits){
            int num = bits - result.length();
            for (int i = 0; i < num; i++) {
                result.insert(0, "0");
            }
        }else {
            return result.substring(result.length() - bits);
        }
        return result.toString();
    }


    // 数字转二进制字符串 ， bits 位数，不足补0
    public static String intToBinary(int decimal , int bits) {
        StringBuilder binaryString = new StringBuilder(Integer.toBinaryString(decimal));
        if (binaryString.length() < bits) {
            int num = bits - binaryString.length();
            for (int i = 0; i < num; i++) {
                binaryString.insert(0, "0");
            }
        }else {
            return binaryString.substring(binaryString.length() - bits);
        }

        return binaryString.toString();
    }


    // 16 进制字符串转二进制字符串
    public static String hexToBinary(String str,int bits) {
        int hexToInt = hexToInt(str);
        return intToBinary(hexToInt,bits);
    }

    // 将整数转化为指定位数的16进制字符串
    public static String intToHexString(int decimal , int bits) {
        StringBuilder hexString = new StringBuilder(Integer.toHexString(decimal));
        if (hexString.length() < bits) {
            int num = bits - hexString.length();
            for (int i = 0; i < num; i++) {
                hexString.insert(0, "0");
            }
        }else {
            return hexString.substring(hexString.length() - bits); // 截取指定位数
        }
        return hexString.toString();
    }

    // 将 Long 转成 16 进制字符串
    public static String longToHexString(long decimal , int bits) {
        StringBuilder hexString = new StringBuilder(Long.toHexString(decimal));
        if (hexString.length() < bits) {
            int num = bits - hexString.length();
            for (int i = 0; i < num; i++) {
                hexString.insert(0, "0");
            }
        }else {
            return hexString.substring(hexString.length() - bits); // 截取指定位数
        }
        return hexString.toString();
    }

    // 将2进制转化为10进制
    public static int binaryToInt(String str) {
        return Integer.parseInt(str,2);
    }

    // 将 byte转换为 16进制字符串
    public static String byteToHexString(byte value) {
        return Integer.toHexString(value & 0xFF);
    }

    public static String bytesToHexString(byte[] bytes , int bits){
        StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < bits; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF); // 将 byte转换为 16进制字符串
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex.toUpperCase());
            if (i != bits-1){
                hexString.append(" ");
            }
        }
        return hexString.toString();
    }

}
